package com.guli.sms.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.List;

import com.guli.common.pojo.vo.SecKillSessionAndRelationVo;
import com.guli.common.valid.SaveGroup;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.guli.common.pojo.SeckillSessionEntity;
import com.guli.sms.service.SeckillSessionService;

import com.guli.common.utils.R;


/**
 * 秒杀活动场次
 *
 * @author LuoJunHua
 * @email 1007052116@qq.com
 * @date 2020-11-29 10:59:49
 */
@RestController
@RequestMapping("/sms/seckillsession")
public class SeckillSessionController {

    @Autowired
    private SeckillSessionService seckillSessionService;


    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取seckillSession分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size, @RequestParam Map<String, Object> params) {

        Page<SeckillSessionEntity> page = seckillSessionService.queryPage(currentPage, size, params);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取SeckillSession")
    public R info(@PathVariable("id") Long id) {

        SeckillSessionEntity seckillSession = seckillSessionService.getById(id);

        return R.ok().put("seckillSession", seckillSession);
    }

    @PostMapping("/save")
    @ApiOperation("插入SeckillSession")
    public R save(@RequestBody @Validated(SaveGroup.class) SeckillSessionEntity seckillSession) {

        seckillSessionService.saveBatchAndCheckIfNeedToRefreshRedisCache(Collections.singletonList(seckillSession));

        return R.ok();
    }

    @PostMapping("/saveBatch")
    @ApiOperation("批量插入SeckillSession")
    @Transactional
    public R saveBatch(@RequestBody @Validated(SaveGroup.class) List<SeckillSessionEntity> seckillSessionList) {

        seckillSessionService.saveBatchAndCheckIfNeedToRefreshRedisCache(seckillSessionList);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新SeckillSession")
    public R update(@RequestBody SeckillSessionEntity seckillSession) {

        seckillSessionService.updateBatchByIdAndCheckIfNeedToRefreshRedisCache(Collections.singletonList(seckillSession));

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除SeckillSession")
    public R delete(@RequestBody Long[] ids) {
        seckillSessionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getLatestSecKillSkuByDateInterval")
    @ApiOperation("获取最近时间的秒杀商品")
    public List<SecKillSessionAndRelationVo> getLatestSecKillSkuByDateInterval(@RequestParam("days") @ApiParam("最近几天") Integer days) {

        List<SecKillSessionAndRelationVo> secKillSkuList = seckillSessionService.getLatestSecKillSkuByDateInterval(days);

        return secKillSkuList;
    }

}
